package com.ruoyi.dance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.service.OssService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.dance.domain.bo.DanceInformationBo;
import com.ruoyi.dance.domain.vo.DanceInformationVo;
import com.ruoyi.dance.domain.DanceInformation;
import com.ruoyi.dance.mapper.DanceInformationMapper;
import com.ruoyi.dance.service.IDanceInformationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 新闻资讯Service业务层处理
 *
 * @author hly
 * @date 2024-01-21
 */
@RequiredArgsConstructor
@Service
public class DanceInformationServiceImpl implements IDanceInformationService {

    private final DanceInformationMapper baseMapper;
    private final OssService ossService;
    private final ISysOssService iSysOssService;

    /**
     * 查询新闻资讯
     */
    @Override
    public DanceInformationVo queryById(Long infoId){
        DanceInformationVo informationVo = baseMapper.selectVoById(infoId);
        informationVo.setImageUrl(ossService.selectUrlByIds(informationVo.getImage()));
        return informationVo;
    }

    /**
     * 查询新闻资讯列表
     */
    @Override
    public TableDataInfo<DanceInformationVo> queryPageList(DanceInformationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DanceInformation> lqw = buildQueryWrapper(bo);
        Page<DanceInformationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        for (DanceInformationVo informationVo : result.getRecords()) {
            informationVo.setImageUrl(ossService.selectUrlByIds(informationVo.getImage()));
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询新闻资讯列表
     */
    @Override
    public List<DanceInformationVo> queryList(DanceInformationBo bo) {
        LambdaQueryWrapper<DanceInformation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DanceInformation> buildQueryWrapper(DanceInformationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DanceInformation> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), DanceInformation::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getTitle()), DanceInformation::getTitle, bo.getTitle());
        lqw.like(StringUtils.isNotBlank(bo.getKeywords()), DanceInformation::getKeywords, bo.getKeywords());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), DanceInformation::getStatus, bo.getStatus());
        lqw.select(
            DanceInformation::getInfoId,
            DanceInformation::getIntro,
            DanceInformation::getImage,
            DanceInformation::getPageView,
            DanceInformation::getKeywords,
            DanceInformation::getTitle,
            DanceInformation::getType,
            DanceInformation::getStatus,
            DanceInformation::getCreateBy,
            DanceInformation::getCreateTime,
            DanceInformation::getUpdateTime,
            DanceInformation::getUpdateBy
        );
        return lqw;
    }

    /**
     * 新增新闻资讯
     */
    @Override
    public Boolean insertByBo(DanceInformationBo bo) {
        DanceInformation add = BeanUtil.toBean(bo, DanceInformation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setInfoId(add.getInfoId());
        }
        return flag;
    }

    /**
     * 修改新闻资讯
     */
    @Override
    public Boolean updateByBo(DanceInformationBo bo) {
        DanceInformation update = BeanUtil.toBean(bo, DanceInformation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DanceInformation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除新闻资讯
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        List<Long> collect = baseMapper.selectVoBatchIds(ids).stream()
            .map(n -> Long.parseLong(n.getImage())).collect(Collectors.toList());
        if (collect.size() > 0) {
            iSysOssService.deleteWithValidByIds(collect, false);
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
